<?php
require_once("libs/general_inc.php");
@session_start();

class charge extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $sql = new sql();
        $sql->connect();
        $this->view->ErrorMessage = "";
        $this->view->post_price = "";
        $GENOBJ = new generalFunction();
        $ProductData = $sql->fetchAll("tbl_product", array("prd_id" => 17));
        if (isset($_POST['plan'])) {

            function IsEmpty($value = '') {
                if (trim($value) == '') {
                    return true;
                }
                return false;
            }

            function isvalidQuantity($n) {
                
                
                if (!preg_match("/^[0-9]+(.[0-9]+)?$/", $n)) {
                    return false;
                }
                
                if ($n <= 0) {
                    return false;
                }
                return true;
            }

            $explode_price = explode("$", $_POST['price']);
            //



            $post_price = $explode_price[1];
            $errorMessage = Array();

            if (IsEmpty($post_price)) {
                $errorMessage = "Price cannot be blank";
            } else {
                if (!isvalidQuantity($post_price)) {
                    $errorMessage = "Price should be a number greater than zero";
                }
            }

            if (count($errorMessage) > 0) {
                $this->view->post_price = $post_price;
                $this->view->ErrorMessage = $errorMessage;
            } else {
                $prd_id = $ProductData[0]['prd_id'];
                $prd_name = $ProductData[0]['prd_name'];
                $prd_qty = 1;
                $prd_cat_id = $ProductData[0]['prd_cat_id'];
                $prd_short_desc = $ProductData[0]['prd_short_desc'];
                $prd_desc = $ProductData[0]['prd_description'];
                $quick_book_id = $ProductData[0]['quick_book_id'];
                $prd_image = $ProductData[0]['prd_image'];
                $prd_code = $ProductData[0]['prd_code'];
                $prd_status = $ProductData[0]['prd_status'];
                $prd_price = $ProductData[0]['prd_price'];
                $prd_name = $ProductData[0]['prd_name'];
                unset($_SESSION['shoping_cart']);

                $cart = $_SESSION['shoping_cart'];
                $cnt = 0;

                /* $product_qty = 1;
                  if(count($cart) > 0) {
                  foreach ($cart as $subKey => $subArray) {
                  if ($subArray['prd_id'] == $prd_id) {
                  $cart[$subKey]['prd_qty'] = $cart[$subKey]['prd_qty'] + $product_qty;
                  $cnt++;
                  }
                  }
                  $_SESSION['shoping_cart'] = $cart;
                  //header('Location: ' . URL . 'cart');
                  }
                 */
                if ($cnt == 0) {
                    $cart[] = array(
                        'prd_id' => $prd_id,
                        'prd_name' => $prd_name,
                        'prd_qty' => $prd_qty,
                        'prd_cat_id' => $prd_cat_id,
                        'prd_shortdesc' => htmlspecialchars_decode($prd_short_desc),
                        'prd_desc' => htmlspecialchars_decode($prd_desc),
                        'quick_book_id' => $quick_book_id,
                        'prd_price' => $post_price,
                        'prd_image' => $prd_image,
                        'prd_code' => $prd_code,
                        'prd_status' => $prd_status,
                    );
                    $filterArray = Array("TrimElement", "htmlEntity");  
                    $filterElements = $GENOBJ->filter($cart, $filterArray);
                    $_SESSION['shoping_cart'] = $filterElements;

                    header('Location: ' . URL . 'cart');
                    //$this->view->successMessage = "Product add to cart.";
                }
            }
        }


        //$this->view->productArray = $productArray;
        $this->view->render('charge');
    }

}
